#pragma once
#include <queue>
#include "OgreExSafeLock.h"
namespace OgreEx
{

	using std::queue;
	template <class T>
	class SafeQueue
	{
	public:
		SafeQueue();
		~SafeQueue();

	public:
		inline bool empty();
		inline void push(const T& t);
		inline void pop(T& t);
		inline void popAll(std::queue<T>& tempqueue);
		inline void clear();

	protected:
		Mutex mMutex;
		std::queue<T> mQueue;
	};

	template <class T>
	SafeQueue<T>::SafeQueue() : mMutex(),mQueue()
	{
	}


	template <class T>
	SafeQueue<T>::~SafeQueue()
	{
		clear();
	}

	template <class T>
	inline bool SafeQueue<T>::empty() 
	{
		bool bempty = false;
		{
			Lock lk(mMutex);
			bempty = mQueue.empty();
		}
		return bempty;
	}

	template <class T>
	inline void SafeQueue<T>::push(const T& t)
	{
		Lock lk(mMutex);
		mQueue.push(t);
	}

	template <class T>
	inline void SafeQueue<T>::pop(T& t)
	{
		Lock lk(mMutex);
		t = mQueue.front();
		mQueue.pop();
	}

	template <class T>
	inline void SafeQueue<T>::popAll(std::queue<T>& tempqueue)
	{
		Lock lk(mMutex);
		tempqueue.c = mQueue.c;
		mQueue.c.clear();
	}

	template <class T>
	inline void SafeQueue<T>::clear()
	{
		Lock lk(mMutex);
		mQueue.c.clear();
	}

}